{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2  九×九乘法口诀表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def print_multiplication_table():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}*{i}={i * j}\", end=\"\\t\")\n",
    "        print()  \n",
    "print_multiplication_table()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4  输入任意正整数，输出分解质因数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15 的质因数分解是: [3, 5]\n"
     ]
    }
   ],
   "source": [
    "def prime_factors(n):\n",
    "    factors = []\n",
    "    while n % 2 == 0:\n",
    "        factors.append(2)\n",
    "        n = n // 2\n",
    "    for i in range(3, int(n**0.5) + 1, 2):\n",
    "        while n % i == 0:\n",
    "            factors.append(i)\n",
    "            n = n // i\n",
    "    if n > 2:\n",
    "        factors.append(n)\n",
    "    return factors\n",
    "num = int(input(\"请输入一个正整数: \"))\n",
    "print(f\"{num} 的质因数分解是: {prime_factors(num)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6  输入一个不多于6位的正整数，要求：求它是几位数，逆序打印出各位数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这个数是5位数。\n",
      "逆序打印各位数字为: 45861\n"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    num_str = input(\"请输入一个不多于6位的正整数: \")\n",
    "    if num_str.isdigit() and 0 < int(num_str) <= 999999:\n",
    "        num_digits = len(num_str)\n",
    "        reversed_num_str = num_str[::-1]\n",
    "        print(f\"这个数是{num_digits}位数。\")\n",
    "        print(f\"逆序打印各位数字为: {reversed_num_str}\")\n",
    "    else:\n",
    "        print(\"输入无效，请输入一个不多于6位的正整数。\")\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8 求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "表达式 s = 5 + 55 + 555 + ... (共5项) 的值是: 61725\n",
      "实际的计算结果是: 61725\n"
     ]
    }
   ],
   "source": [
    "def calculate_series_sum(a, n):\n",
    "    total_sum = 0\n",
    "    current_term = 0 \n",
    "    for i in range(1, n + 1): \n",
    "        current_term = current_term * 10 + a\n",
    "        total_sum += current_term\n",
    "    return total_sum\n",
    "a = int(input(\"请输入一个数字a: \"))\n",
    "n = int(input(\"请输入相加的项数n: \"))\n",
    "result = calculate_series_sum(a, n)\n",
    "print(f\"表达式 s = {a} + {a*10+a} + {a*100+a*10+a} + ... (共{n}项) 的值是: {result}\")\n",
    "print(f\"实际的计算结果是: {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数列的前2项之和是: 49/15\n"
     ]
    }
   ],
   "source": [
    "def fraction_sum(n):\n",
    "    num1, den1 = 2, 1\n",
    "    num2, den2 = 3, 2\n",
    "    total_num = 0\n",
    "    total_den = 1\n",
    "    for _ in range(n):\n",
    "        current_num = num1 + num2\n",
    "        current_den = den1 + den2\n",
    "        total_num = total_num * current_den + current_num * total_den\n",
    "        total_den *= current_den\n",
    "        num1, den1 = num2, den2\n",
    "        num2, den2 = current_num, current_den\n",
    "    from math import gcd\n",
    "    common_divisor = gcd(total_num, total_den)\n",
    "    simplified_num = total_num // common_divisor\n",
    "    simplified_den = total_den // common_divisor\n",
    "    return simplified_num, simplified_den\n",
    "n = int(input(\"请输入要计算的项数N: \"))\n",
    "result_num, result_den = fraction_sum(n)\n",
    "print(f\"数列的前{n}项之和是: {result_num}/{result_den}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12  一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "满足条件的整数是: None\n"
     ]
    }
   ],
   "source": [
    "def find_x():\n",
    "    factors = [(1, 268), (2, 134), (4, 67), (134, 2), (67, 4), (268, 1)] \n",
    "    for factor1, factor2 in factors:\n",
    "        if factor1 > factor2:\n",
    "            factor1, factor2 = factor2, factor1\n",
    "        m = (factor1 + factor2) // 2\n",
    "        n = (factor2 - factor1) // 2\n",
    "        if m ** 2 - 100 >= 0:\n",
    "            x = m ** 2 - 100\n",
    "            if int((x + 368) ** 0.5) ** 2 == x + 368:\n",
    "                return x\n",
    "    return None\n",
    "x = find_x()\n",
    "print(f\"满足条件的整数是: {x}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14  有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1个月的兔子总数为: 1对\n",
      "第2个月的兔子总数为: 1对\n",
      "第3个月的兔子总数为: 2对\n",
      "第4个月的兔子总数为: 3对\n",
      "第5个月的兔子总数为: 5对\n"
     ]
    }
   ],
   "source": [
    "def calculate_rabbit_populations(months):\n",
    "    populations = [0] * (months + 1)  \n",
    "    if months >= 1:\n",
    "        populations[1] = 1\n",
    "    if months >= 2:\n",
    "        populations[2] = 1\n",
    "    for month in range(3, months + 1):\n",
    "        populations[month] = populations[month - 1] + populations[month - 2]\n",
    "    return populations[1:]  \n",
    "months = int(input(\"请输入要计算的月份数: \"))\n",
    "populations = calculate_rabbit_populations(months)\n",
    "for month, population in enumerate(populations, start=1):\n",
    "    print(f\"第{month}个月的兔子总数为: {population}对\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16  任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个3x3的矩阵，每行输入3个数字，用空格分隔:\n",
      "主对角线元素之和为: 41\n",
      "副对角线元素之和为: 46\n"
     ]
    }
   ],
   "source": [
    "def input_matrix():\n",
    "    matrix = []\n",
    "    print(\"请输入一个3x3的矩阵，每行输入3个数字，用空格分隔:\")\n",
    "    for i in range(3):\n",
    "        row = list(map(int, input().split()))\n",
    "        if len(row) != 3:\n",
    "            print(f\"第{i+1}行应该输入3个数字，请重新输入。\")\n",
    "            i -= 1\n",
    "            continue\n",
    "        matrix.append(row)\n",
    "    return matrix\n",
    "def diagonal_sum(matrix):\n",
    "    main_diagonal_sum = 0\n",
    "    secondary_diagonal_sum = 0\n",
    "    for i in range(3):\n",
    "        main_diagonal_sum += matrix[i][i]\n",
    "    for i in range(3):\n",
    "        secondary_diagonal_sum += matrix[i][2 - i]\n",
    "    return main_diagonal_sum, secondary_diagonal_sum\n",
    "matrix = input_matrix()\n",
    "main_sum, secondary_sum = diagonal_sum(matrix)\n",
    "print(f\"主对角线元素之和为: {main_sum}\")\n",
    "print(f\"副对角线元素之和为: {secondary_sum}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18  有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6, 7, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "def rotate_list(nums, m):\n",
    "    if not nums or m == 0:\n",
    "        return nums\n",
    "    n = len(nums)\n",
    "    m = m % n\n",
    "    rotated_nums = nums[-m:] + nums[:-m]\n",
    "    return rotated_nums\n",
    "nums = [1, 2, 3, 4, 5, 6, 7]\n",
    "m = 3\n",
    "rotated = rotate_list(nums, m)\n",
    "print(rotated) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "20  编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数列和为: 0.5\n"
     ]
    }
   ],
   "source": [
    "def sum_even_series(n):\n",
    "    \"\"\"计算偶数项数列的和: 1/2 + 1/4 + ... + 1/n\"\"\"\n",
    "    return sum(1 / i for i in range(2, n + 1, 2))\n",
    "def sum_odd_series(n):\n",
    "    \"\"\"计算奇数项数列的和: 1/1 + 1/3 + ... + 1/n\"\"\"\n",
    "    return sum(1 / i for i in range(1, n + 1, 2))\n",
    "def calculate_series_sum(n):\n",
    "    \"\"\"根据n的奇偶性调用相应的函数来计算数列和\"\"\"\n",
    "    if n % 2 == 0:\n",
    "        return sum_even_series(n)\n",
    "    else:\n",
    "        return sum_odd_series(n)\n",
    "n = int(input(\"请输入一个正整数n: \"))\n",
    "result = calculate_series_sum(n)\n",
    "print(f\"数列和为: {result}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
